package iwhr.nsbd.util;

import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;

public class GetDayOfMonth {
    /**
     * 获取本月天数
     */
    public int getDaysOfMonth() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前月份
        int currentMonth = currentDate.getMonthValue();
        // 获取当前年份
        int currentYear = currentDate.getYear();
        // 构建 YearMonth 对象
        YearMonth yearMonth = YearMonth.of(currentYear, currentMonth);
        // 获取当月的天数
        int daysInMonth = yearMonth.lengthOfMonth();
        return daysInMonth;

    }

    public int getDaysInMonth(int month) {
        int year = Year.now().getValue();
        switch (month) {
            case 2:
                if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
                    return 29;
                } else {
                    return 28;
                }
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            default:
                return 31;
        }
    }
}
